<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-3-19
 * Time: 23:56
 */

namespace app\admin\controller;

use app\admin\model\Toolslist as tools;
use think\Db;

class Toolsmanage extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    public function getlist($page=1,$limit=10,$search=''){
        $where = "(1=1)";

        $lists = tools::where($where)->page($page,$limit)->select();
        foreach ($lists as $k=>$list){
            $lists[$k]['type_span'] = '<span class="layui-badge layui-bg-orange">'. getToolsType($list['type']) .'</span>';
        }
        $lists_count = tools::where($where)->count();
        return ['code'=>0,'data'=>$lists,'msg'=>'','count'=>$lists_count];
    }

    public function edit($id=''){
        $form = tools::get($id);
        $this->assign('form',$form);
        return $this->fetch();
    }

    public function save($id=''){
        Db::startTrans();
        try{
            $rs = Db::table('tools_list');
            if(empty($id)){
                $_POST['create_user'] = 'alan.yue';
                $_POST['create_time'] = get_datetime();
                $rs->insert($_POST);
            }else{
                $_POST['update_user'] = 'alan.yue';
                $_POST['update_time'] = get_datetime();
                $rs->where('id',$id)->update($_POST);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error('保存失败！<br>Error：'.$e->getMessage());
        }
        $this->success('保存成功！');
    }
}